package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.beans.SupportUserBean;

import java.util.HashMap;
import java.util.List;

import static com.chuanye.mydrawable.adapters.SpinnerOneAdapter.setListViewHeightBasedOnChildren;

/**
 * Created by lsef on 2019/2/14.
 */

public class SupportUserOneAdapter extends BaseAdapter{
    private Context mContext;
    private List<SupportUserBean.ObjBean> objBeanList;
    private List<SupportUserBean.ObjBean.ValueBean> valueBeanList2;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    public SupportUserOneAdapter(Context mContext, List<SupportUserBean.ObjBean> objBeanList,HashMap<Integer,Boolean> isSelected,List<SupportUserBean.ObjBean.ValueBean> valueBeanList2) {
        this.mContext = mContext;
        this.objBeanList = objBeanList;
        this.isSelected = isSelected;
        this.valueBeanList2 = valueBeanList2;
    }

    @Override
    public int getCount() {
        return objBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return objBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.supuserone_item_view,null);
            holder = new ViewHolder();
            holder.name = view.findViewById(R.id.supportuserone_tv);
            holder.lv = view.findViewById(R.id.supportuserone_lv);
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        holder.name.setText(objBeanList.get(i).getKey());
        SupportUserTwoAdapter adapter = new SupportUserTwoAdapter(mContext,objBeanList.get(i).getValue(),isSelected,valueBeanList2);
        holder.lv.setAdapter(adapter);
        /*使用这个方法来获取listview的高度，需要注意一下几个问题：
        1、listview的item的根布局一定要是LinearLayout；
        2、调用这个方法需要在适配器数据加载更新之后；代码如下：*/
        adapter.notifyDataSetChanged();
        setListViewHeightBasedOnChildren(holder.lv);
        return view;
    }
    private static class ViewHolder{
        TextView name;
        ListView lv;
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition
            return;
        }
        int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView); // listItem.measure(0, 0);
            listItem.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}
